//this will be used simulate the read in for the bluetooth data
//Made by Jason Furente for Autolytics and the AutoBiography Project

import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.sql.*;

public class bluereader
{
  Connection dbConnection;
  Statement stmnt;
  Car currentCar;
	public static void main (String[] args) 
	{
		System.out.println("Hello World");

		//bluetoothreader("test");
		
		for(int i=0; i<car_dat.size(); i++)
		{
			System.out.println(car_dat.get(i));
		}
		//split();
		//System.out.println(car_vin + " "+ speed + " "+ dist + " "+ temp + " "+ horse);
		
		//car_vin="abbb1212";
		//speed=777;	
		//combine_add();
		
		
	}

	public static ArrayList<String> car_dat= new ArrayList<String>();
  public bluereader(){
  }
  public bluereader(String filename, Connection con, Car currCar){
    ArrayList<String> foo = bluetoothreader(filename, con, currCar);
  }
  public ArrayList<String> bluetoothreader(String filename, Connection con, Car currCar){
    dbConnection = con;

    currentCar = currCar;
    new phonyblue(filename);
    String rUser, rPass, slot;
    try{
      File statFile = new File(filename);
      Scanner inScan = new Scanner(statFile);				
      inScan.useDelimiter(";");
      if ((slot = inScan.next()) != null){
        split(slot);
      }
      
    } catch (Exception e){System.err.println("File reading error: " + e.getMessage());}
    return car_dat;
  }
  public String make, model, year, car_vin, data, transStatus, infoStatus, infoConnected, CD, fanSpeed, climStatus, phone, comStatus, headlight="", internLights, powerOutlets, suspStatus, brakeStatus, steerStatus, ipod;
  public int speed, dist, temp, RPM, Mileage, IntTemp, ExtTemp, callTime, brakeFluidLife, tirePressure;
  public double FuelEconomy, FuelP, OilLife, radf, N, D, R, G1, G2, G3;

  public void split(String s)
  {
    Calendar myCal = Calendar.getInstance();
    try{
      stmnt = dbConnection.createStatement();
    }catch(SQLException e){
      System.out.println("Statement creation error: "+e);
    }
    
    String slot = s;
    
    StringTokenizer slots;
    
    slots = new StringTokenizer(slot, " ");
    
    make = slots.nextToken();
    model = slots.nextToken();
    year = slots.nextToken();
    car_vin=slots.nextToken();
    //Engine:
    RPM=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(RPM));

    temp=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(temp));

    tirePressure=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(tirePressure));

    FuelEconomy=Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(FuelEconomy));
    
    Mileage=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(Mileage));
    
    OilLife= Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(OilLife));

    FuelP= Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(FuelP));

    //Transmission:
    transStatus=slots.nextToken();
    car_dat.add(transStatus);

    N=Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(N));

    D=Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(D));

    R=Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(R));

    G1=Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(G1));    

    G2= Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(G2));  

    G3=Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(G3));

    //Infotainment:
    infoStatus=slots.nextToken();
    car_dat.add(infoStatus);

    radf=Double.parseDouble(slots.nextToken());
    car_dat.add(Double.toString(radf));

    infoConnected=slots.nextToken();
    car_dat.add(infoConnected);

    CD=slots.nextToken();
    car_dat.add(CD);

    ipod=slots.nextToken();
    car_dat.add(ipod);

    //Climate:
    IntTemp=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(IntTemp));   

    ExtTemp=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(ExtTemp));    

    fanSpeed=slots.nextToken();
    car_dat.add(fanSpeed);

    climStatus=slots.nextToken();
    car_dat.add(climStatus);

    //Communication:
    phone=slots.nextToken();
    car_dat.add(phone);

    callTime=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(callTime));    

    comStatus=slots.nextToken();
    car_dat.add(comStatus);

    //Lighting/Electrical:
    headlight=slots.nextToken();
    car_dat.add(headlight);

    powerOutlets=slots.nextToken();
    car_dat.add(powerOutlets);

    internLights=slots.nextToken();
    car_dat.add(internLights);

    //Suspension, Brakes, Tires, and Steering:
    suspStatus=slots.nextToken();
    car_dat.add(suspStatus);

    brakeFluidLife=Integer.parseInt(slots.nextToken());
    car_dat.add(Integer.toString(brakeFluidLife));

    brakeStatus=slots.nextToken();
    car_dat.add(brakeStatus);

    steerStatus=slots.nextToken();
    car_dat.add(steerStatus);
    
    String month="", amPm;
    int mo=myCal.get(Calendar.MONTH), aP=myCal.get(Calendar.AM_PM);
    
    if(aP==0){
      amPm = "AM";
    }
    else{
      amPm = "PM";
    }

    switch(mo){
    case 1: month = "JAN"; break;
    case 2: month = "FEB"; break;
    case 3: month = "MAR"; break;
    case 4: month = "APR"; break;
    case 5: month = "MAY"; break;
    case 6: month = "JUN"; break;
    case 7: month = "JUL"; break;
    case 8: month = "AUG"; break;
    case 9: month = "SEP"; break;
    case 10: month = "OCT"; break;
    case 11: month = "NOV"; break;
    case 12: month = "DEC"; break;
    }


    try{
        //
      ResultSet rs = stmnt.executeQuery("SELECT * FROM STATS WHERE VIN = '" +currentCar.vin+"' ORDER BY 'ORDER_NUM' ASC");
        //choice = 20
      int nextOrder;
      if(rs.next()){
        rs.last();
        nextOrder = rs.getInt("ORDER_NUM");
        System.out.println(nextOrder);
      }
      else
        nextOrder = 0;
      nextOrder++;

      String dbVIN;
      if(currentCar == null){
        dbVIN = car_vin;
      }
      else{
        dbVIN = currentCar.vin;
      }

      String updateString = "INSERT INTO STATS (RPM, ENGINE_TEMP, TIRE_PRESSURE, FUEL_ECONOMY, MILEAGE, OIL_LIFE, FUEL_LEVEL, TRANS_STATUS, TIME_IN_N, TIME_IN_D, TIME_IN_R, TIME_IN_1, TIME_IN_2, TIME_IN_3, INFO_STATUS, RADIO_FREQ, CONNECTED, CD, IPOD, INTERNAL_TEMP, EXTERNAL_TEMP, FAN_SPEED, CLIMATE_STATUS, PHONE_CON, CALL_TIME, COM_STATUS, HEADLIGHT_STATUS, POWER_OUTLETS, INTERNAL_LIGHTS, SUSP_STATUS, BRAKE_FLUID_LIFE, BRAKE_STATUS, STEERING_STATUS, ORDER_NUM, DATE_TIME, VIN) VALUES ("+RPM+", "+temp+", "+tirePressure+", "+FuelEconomy+", "+Mileage+", "+OilLife+", "+FuelP+", '"+transStatus+"', "+N+", "+D+", "+R+", "+G1+", "+G2+", "+G3+", '"+infoStatus+"', "+radf+", '"+infoConnected+"', '"+CD+"', '"+ipod+"', "+IntTemp+", "+ExtTemp+", '"+fanSpeed+"', '"+climStatus+"', '"+phone+"', "+callTime+", '"+comStatus+"', '"+headlight+"', '"+powerOutlets+"', '"+internLights+"', '"+suspStatus+"', "+brakeFluidLife+", '"+brakeStatus+"', '"+steerStatus+"', "+nextOrder+", '"+myCal.get(Calendar.HOUR)+":"+myCal.get(Calendar.MINUTE)+":"+myCal.get(Calendar.SECOND)+" "+amPm+" "+month+" "+myCal.get(Calendar.DATE)+" "+myCal.get(Calendar.YEAR)+"', '"+dbVIN+"')";
      stmnt.executeUpdate(updateString);
    }catch(SQLException e1){
      System.out.println("DB Upload error: "+e1);
    }
  }
  
  public String[] getMMY(){
    String[] maMoYr = {make, model, year, car_vin};
    return maMoYr;
  }
}
